﻿using System;
using System.Collections.Generic;
using UnityEngine;
using XUpdater;

namespace XUtliPoolLib
{
	// Token: 0x02000049 RID: 73
	public class AssetBundleInfo
	{
		// Token: 0x1700005D RID: 93
		// (get) Token: 0x0600023B RID: 571 RVA: 0x00012853 File Offset: 0x00010A53
		// (set) Token: 0x0600023C RID: 572 RVA: 0x0001285B File Offset: 0x00010A5B
		[SerializeField]
		public int refCount { get; private set; }

		// Token: 0x0600023E RID: 574 RVA: 0x00012894 File Offset: 0x00010A94
		public void AddDependency(AssetBundleInfo target)
		{
			bool flag = target != null;
			if (flag)
			{
				bool flag2 = this.deps == null;
				if (flag2)
				{
					this.deps = new HashSet<AssetBundleInfo>();
				}
				bool flag3 = this.deps.Add(target);
				if (flag3)
				{
					target.Retain();
					bool flag4 = target.depChildren == null;
					if (flag4)
					{
						target.depChildren = new List<uint>();
					}
					target.depChildren.Add(this.bundleName);
				}
			}
		}

		// Token: 0x0600023F RID: 575 RVA: 0x00012908 File Offset: 0x00010B08
		public void ResetLifeTime()
		{
			bool isReady = this._isReady;
			if (isReady)
			{
				this._readyTime = Time.time;
			}
		}

		// Token: 0x06000240 RID: 576 RVA: 0x00012930 File Offset: 0x00010B30
		public void Retain()
		{
			int refCount = this.refCount;
			this.refCount = refCount + 1;
		}

		// Token: 0x06000241 RID: 577 RVA: 0x00012950 File Offset: 0x00010B50
		public void Release()
		{
			int refCount = this.refCount;
			this.refCount = refCount - 1;
		}

		// Token: 0x06000242 RID: 578 RVA: 0x00012970 File Offset: 0x00010B70
		public void Retain(UnityEngine.Object owner)
		{
			bool flag = owner == null;
			if (flag)
			{
				throw new Exception("Please set the user!");
			}
			for (int i = 0; i < this.references.Count; i++)
			{
				bool flag2 = owner.Equals(this.references[i].Target);
				if (flag2)
				{
					return;
				}
			}
			WeakReference item = new WeakReference(owner);
			this.references.Add(item);
		}

		// Token: 0x06000243 RID: 579 RVA: 0x000129E4 File Offset: 0x00010BE4
		public void Release(object owner)
		{
			for (int i = 0; i < this.references.Count; i++)
			{
				bool flag = this.references[i].Target == owner;
				if (flag)
				{
					this.references.RemoveAt(i);
					break;
				}
			}
		}

		// Token: 0x06000244 RID: 580 RVA: 0x00012A38 File Offset: 0x00010C38
		public virtual GameObject Instantiate()
		{
			return this.Instantiate(true);
		}

		// Token: 0x06000245 RID: 581 RVA: 0x00012A54 File Offset: 0x00010C54
		public virtual GameObject Instantiate(bool enable)
		{
			bool flag = this.mainObject != null;
			if (flag)
			{
				bool flag2 = this.mainObject is GameObject;
				if (flag2)
				{
					GameObject gameObject = this.mainObject as GameObject;
					gameObject.SetActive(enable);
					GameObject gameObject2 = XCommon.Instantiate<GameObject>(gameObject);
					gameObject2.name = gameObject.name;
					this.Retain(gameObject2);
					return gameObject2;
				}
			}
			return null;
		}

		// Token: 0x06000246 RID: 582 RVA: 0x00012AC4 File Offset: 0x00010CC4
		public virtual GameObject Instantiate(Vector3 position, Quaternion rotation, bool enable = true)
		{
			bool flag = this.mainObject != null;
			if (flag)
			{
				bool flag2 = this.mainObject is GameObject;
				if (flag2)
				{
					GameObject gameObject = this.mainObject as GameObject;
					gameObject.SetActive(enable);
					GameObject gameObject2 = UnityEngine.Object.Instantiate<GameObject>(gameObject, position, rotation, null);
					gameObject2.name = gameObject.name;
					this.Retain(gameObject2);
					return gameObject2;
				}
			}
			return null;
		}

		// Token: 0x06000247 RID: 583 RVA: 0x00012B38 File Offset: 0x00010D38
		public UnityEngine.Object Require(UnityEngine.Object user)
		{
			this.Retain(user);
			return this.mainObject;
		}

		// Token: 0x06000248 RID: 584 RVA: 0x00012B58 File Offset: 0x00010D58
		public UnityEngine.Object Require(Component c, bool autoBindGameObject)
		{
			bool flag = autoBindGameObject && c && c.gameObject;
			UnityEngine.Object result;
			if (flag)
			{
				result = this.Require(c.gameObject);
			}
			else
			{
				result = this.Require(c);
			}
			return result;
		}

		// Token: 0x06000249 RID: 585 RVA: 0x00012BA0 File Offset: 0x00010DA0
		private int UpdateReference()
		{
			for (int i = 0; i < this.references.Count; i++)
			{
				UnityEngine.Object exists = (UnityEngine.Object)this.references[i].Target;
				bool flag = !exists;
				if (flag)
				{
					this.references.RemoveAt(i);
					i--;
				}
			}
			return this.references.Count;
		}

		// Token: 0x1700005E RID: 94
		// (get) Token: 0x0600024A RID: 586 RVA: 0x00012C14 File Offset: 0x00010E14
		public bool isUnused
		{
			get
			{
				return this._isReady && Time.time - this._readyTime > this.minLifeTime && this.refCount <= 0 && this.UpdateReference() == 0;
			}
		}

		// Token: 0x0600024B RID: 587 RVA: 0x00012C58 File Offset: 0x00010E58
		public virtual void Dispose()
		{
			this.UnloadBundle();
			bool flag = this.deps != null;
			if (flag)
			{
				foreach (AssetBundleInfo assetBundleInfo in this.deps)
				{
					bool flag2 = assetBundleInfo.depChildren != null;
					if (flag2)
					{
						assetBundleInfo.depChildren.Remove(this.bundleName);
					}
					assetBundleInfo.Release();
				}
				this.deps.Clear();
			}
			this.references.Clear();
			bool flag3 = this.onUnloaded != null;
			if (flag3)
			{
				this.onUnloaded(this);
			}
			bool flag4 = !(this._mainObject is GameObject) && !(this._mainObject is ScriptableObject) && !(this._mainObject is Material);
			if (flag4)
			{
				Resources.UnloadAsset(this._mainObject);
			}
			else
			{
				UnityEngine.Object.Destroy(this._mainObject);
			}
			this._mainObject = null;
		}

		// Token: 0x1700005F RID: 95
		// (get) Token: 0x0600024C RID: 588 RVA: 0x00012D54 File Offset: 0x00010F54
		// (set) Token: 0x0600024D RID: 589 RVA: 0x00012D6C File Offset: 0x00010F6C
		public bool isReady
		{
			get
			{
				return this._isReady;
			}
			set
			{
				this._isReady = value;
			}
		}

		// Token: 0x0600024E RID: 590 RVA: 0x00012D78 File Offset: 0x00010F78
		public AsyncOperation LoadAsync(string name, Type t)
		{
			bool flag = this._mainObject == null && this._isReady;
			AsyncOperation result;
			if (flag)
			{
				result = this.bundle.LoadAssetAsync(name, t);
			}
			else
			{
				result = null;
			}
			return result;
		}

		// Token: 0x0600024F RID: 591 RVA: 0x00012DB8 File Offset: 0x00010FB8
		public void AsyncLoadFinish(UnityEngine.Object asset)
		{
			bool flag = this._mainObject == null && this._isReady;
			if (flag)
			{
				this.Release();
				this._mainObject = asset;
				bool flag2 = this.data.compositeType == AssetBundleExportType.Root;
				if (flag2)
				{
					bool flag3 = XSingleton<XUpdater.XUpdater>.singleton.ABManager != null;
					if (flag3)
					{
						XSingleton<XUpdater.XUpdater>.singleton.ABManager.AddUnloadBundleQueue(this);
					}
				}
			}
		}

		// Token: 0x17000060 RID: 96
		// (get) Token: 0x06000250 RID: 592 RVA: 0x00012E2C File Offset: 0x0001102C
		public virtual UnityEngine.Object mainObject
		{
			get
			{
				bool flag = this._mainObject == null && this._isReady;
				if (flag)
				{
					this._mainObject = this.bundle.LoadAsset(this.bundle.GetAllAssetNames()[0]);
					bool flag2 = this._mainObject != null;
					if (flag2)
					{
						bool flag3 = this.data.compositeType == AssetBundleExportType.Root;
						if (flag3)
						{
							bool flag4 = XSingleton<XUpdater.XUpdater>.singleton.ABManager != null;
							if (flag4)
							{
								XSingleton<XUpdater.XUpdater>.singleton.ABManager.AddUnloadBundleQueue(this);
							}
						}
					}
				}
				return this._mainObject;
			}
		}

		// Token: 0x06000251 RID: 593 RVA: 0x00012ECC File Offset: 0x000110CC
		private T LoadAsset<T>(string name) where T : UnityEngine.Object
		{
			return this.bundle.LoadAsset(name, typeof(T)) as T;
		}

		// Token: 0x06000252 RID: 594 RVA: 0x00012F00 File Offset: 0x00011100
		public void UnloadBundle()
		{
			bool flag = this.bundle != null;
			if (flag)
			{
				bool enableLog = AssetBundleManager.enableLog;
				if (enableLog)
				{
					XSingleton<XDebug>.singleton.AddLog((string.Concat(new object[]
					{
						"Unload : ",
						this.data.compositeType,
						" >> ",
						this.bundleName,
						"(",
						this.data.debugName
					}) != null) ? this.data.debugName : ")", null, null, null, null, null, XDebugColor.XDebug_None);
				}
				this.bundle.Unload(false);
				bool flag2 = XSingleton<XUpdater.XUpdater>.singleton.ABManager != null;
				if (flag2)
				{
					XSingleton<XUpdater.XUpdater>.singleton.ABManager.DeleteBundleCount();
				}
			}
			this.bundle = null;
		}

		// Token: 0x0400021A RID: 538
		public AssetBundleInfo.OnUnloadedHandler onUnloaded;

		// Token: 0x0400021B RID: 539
		internal AssetBundle bundle;

		// Token: 0x0400021C RID: 540
		public uint bundleName;

		// Token: 0x0400021D RID: 541
		public AssetBundleData data;

		// Token: 0x0400021E RID: 542
		public float minLifeTime = 2f;

		// Token: 0x0400021F RID: 543
		private float _readyTime;

		// Token: 0x04000220 RID: 544
		private bool _isReady;

		// Token: 0x04000221 RID: 545
		private UnityEngine.Object _mainObject;

		// Token: 0x04000223 RID: 547
		private HashSet<AssetBundleInfo> deps = null;

		// Token: 0x04000224 RID: 548
		private List<uint> depChildren = null;

		// Token: 0x04000225 RID: 549
		private List<WeakReference> references = new List<WeakReference>();

		// Token: 0x0200029A RID: 666
		// (Invoke) Token: 0x06000DF2 RID: 3570
		public delegate void OnUnloadedHandler(AssetBundleInfo abi);
	}
}
